package com.asurplus.system.controller;


import com.asurplus.common.layui.LayuiTableResult;
import com.asurplus.common.minio.MinioUtils;
import com.asurplus.common.utils.R;
import com.asurplus.system.entity.SysFiles;
import com.asurplus.system.service.SysFilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 系统附件表 前端控制器
 * </p>
 *
 * @author lizhou
 * @since 2021-01-11
 */
@Controller
@RequestMapping("/system/sys-files")
public class SysFilesController {

    @Autowired
    private SysFilesService sysFilesService;
    @Autowired
    private MinioUtils minioUtils;

    @GetMapping("init")
    public String init() {
        return "system/files/list";
    }

    @GetMapping("add")
    public String add() {
        return "system/files/add";
    }

    @GetMapping("update/{id}")
    public String update(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("data", sysFilesService.getById(id));
        return "system/files/update";
    }

    @PostMapping("list")
    @ResponseBody
    public LayuiTableResult list(Integer page, Integer limit, SysFiles sysFiles) {
        return sysFilesService.list(page, limit, sysFiles);
    }

    @PostMapping("add")
    @ResponseBody
    public R add(@RequestBody SysFiles sysFiles) {
        return sysFilesService.add(sysFiles);
    }

    @PostMapping("update")
    @ResponseBody
    public R update(@RequestBody SysFiles sysFiles) {
        return sysFilesService.update(sysFiles);
    }

    @GetMapping("delete/{id}")
    @ResponseBody
    public R delete(@PathVariable("id") Integer id) {
        return sysFilesService.delete(id);
    }

    /**
     * 上传附件
     */
    @PostMapping("upload")
    @ResponseBody
    public R uploadFile(@RequestParam(name = "file") MultipartFile file, @RequestParam(value = "folder", required = false, defaultValue = "default") String folder) {
        return minioUtils.uploadSysFile(file, folder);
    }
}
